﻿using System;
using System.Collections.Generic;

namespace smp.ButterflyCakes.Models.ViewModels.Order
{
    public class EditMenuView
    {
        public EditMenuView(List<EditMenuDataItem> data, Guid activeStage)
        {
            Components = new List<EditMenuComponentView>();

            for (int i = 0; i < data.Count; i++)
            {
                var item = data[i];
                var isActive = false;
                if(activeStage == Guid.Empty && i == data.Count -1)
                {
                    isActive = true;
                }
                else if(activeStage == item.StageId)
                {
                    isActive = true;
                }

                var group = Components.Find(x => x.Title == item.ComponentTitle);
                if (group == null)
                {
                    Components.Add(new EditMenuComponentView(item, isActive));
                }
                else
                {
                    group.Add(item, isActive);
                }
            }
        }

        public List<EditMenuComponentView> Components { get; private set; }
    }

    public class EditMenuComponentView
    {
        public EditMenuComponentView(EditMenuDataItem data, bool isActive)
        {
            Title = data.ComponentTitle;
            Stages = new List<EditMenuStageView> {new EditMenuStageView(data, isActive)};
            IsActive = isActive;
            UiIdName = Guid.NewGuid().ToString();
        }

        public string UiIdName { get; private set; }
        public string Title { get; private set; }
        public bool IsActive { get; private set; }
        public List<EditMenuStageView> Stages { get; private set; }

        public void Add(EditMenuDataItem data, bool isActive)
        {
            Stages.Add(new EditMenuStageView(data, isActive));
            IsActive = isActive;
        }
    }

    public class EditMenuStageView
    {
        public EditMenuStageView(EditMenuDataItem data, bool isActive)
        {
            StageId = data.StageId;
            Title = data.StageTitle;
            IsActive = isActive;
            Stage = data.Stage;
        }

        public Guid StageId { get; private set; }
        public string Title { get; private set; }
        public bool IsActive { get; private set; }
        public Stage Stage { get; private set; }
    }

    public class EditMenuDataItem
    {
        public Guid StageId { get; set; }
        public Stage Stage { get; set; }
        public string StageTitle { get; set; }
        public string ComponentTitle { get; set; }
    }
}
